package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

@Repository("CrawlerfilestandardDAO")
@Transactional
public class CrawlerfilestandardDAOImpl extends AbstractJpaDao<Crawlerfilestandard> implements CrawlerfilestandardDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Crawlerfilestandard.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public CrawlerfilestandardDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Crawlerfilestandard findCrawlerfilestandardById(Integer id) throws DataAccessException {

		return findCrawlerfilestandardById(id, -1, -1);
	}

	@Transactional
	public Crawlerfilestandard findCrawlerfilestandardById(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerfilestandardById", startResult, maxRows, id);
			return (Crawlerfilestandard) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Crawlerfilestandard> findCrawlerfilestandardByName(String name) throws DataAccessException {

		return findCrawlerfilestandardByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilestandard> findCrawlerfilestandardByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfilestandardByName", startResult, maxRows, name);
		return new LinkedHashSet<Crawlerfilestandard>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerfilestandard> findCrawlerfilestandardByNameContaining(String name) throws DataAccessException {

		return findCrawlerfilestandardByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilestandard> findCrawlerfilestandardByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfilestandardByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Crawlerfilestandard>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerfilestandard> findCrawlerfilestandardByDescription(String description)
			throws DataAccessException {

		return findCrawlerfilestandardByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilestandard> findCrawlerfilestandardByDescription(String description, int startResult,
			int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfilestandardByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Crawlerfilestandard>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerfilestandard> findCrawlerfilestandardByDescriptionContaining(String description)
			throws DataAccessException {

		return findCrawlerfilestandardByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilestandard> findCrawlerfilestandardByDescriptionContaining(String description, int startResult,
			int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfilestandardByDescriptionContaining", startResult, maxRows,
				description);
		return new LinkedHashSet<Crawlerfilestandard>(query.getResultList());
	}

	@Transactional
	public Crawlerfilestandard findCrawlerfilestandardByPrimaryKey(Integer id) throws DataAccessException {
		return findCrawlerfilestandardByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Crawlerfilestandard findCrawlerfilestandardByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerfilestandardByPrimaryKey", startResult, maxRows, id);
			return (Crawlerfilestandard) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Crawlerfilestandard> findCrawlerfilestandardsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerfilestandardsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Crawlerfilestandard> findAllCrawlerfilestandards() throws DataAccessException {

		return findAllCrawlerfilestandards(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilestandard> findAllCrawlerfilestandards(int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAllCrawlerfilestandards", startResult, maxRows);
		return new LinkedHashSet<Crawlerfilestandard>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilestandard> findCrawlerfilestandardByCompositeProperities(String sql, int startResult,
			int maxRows) {
		return new LinkedHashSet<Crawlerfilestandard>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Crawlerfilestandard> findCrawlerfilestandardsByDetachedCriteria(CmiPagination page)
			throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Crawlerfilestandard.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Crawlerfilestandard> crawlerfilestandards = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return crawlerfilestandards;
	}

	@Transactional
	public Crawlerfilestandard findCrawlerfilestandardByPhysicalPrimaryKey(String name) throws DataAccessException {
		return findCrawlerfilestandardByPhysicalPrimaryKey(name, -1, -1);
	}

	@Transactional
	public Crawlerfilestandard findCrawlerfilestandardByPhysicalPrimaryKey(String name, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerfilestandardByPhyscialPrimaryKey", startResult, maxRows, name);
			return (Crawlerfilestandard) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Crawlerfilestandard entity) {
		return true;
	}
}
